package com.hh.service.Impl;

import com.hh.dto.ResultDTO;
import com.hh.mapper.ProductMapper;
import com.hh.model.ProductModel;
import com.hh.service.ProductService;
import com.hh.until.JSONCopy;
import com.hh.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Override
    public ResultDTO add(ProductVO productVo) {
        ProductModel productModel = JSONCopy.copy(productVo, ProductModel.class);
/*        int count = productDAO.add(productModel);
        ResultDTO resultDTO = ResultDTO.builder().count(count).build();
        return resultDTO;*/
        return ResultDTO.getDto(productMapper.add(productModel), null);
    }

    @Override
    public ResultDTO delete(ProductVO productVo) {
        ProductModel productModel = JSONCopy.copy(productVo, ProductModel.class);
        /*int count = productDAO.delete(productModel);
        ResultDTO resultDTO = ResultDTO.builder().count(count).build();
        return resultDTO;*/
        return ResultDTO.getDto(productMapper.delete(productModel), null);
    }

    @Override
    public ResultDTO update(ProductVO productVo) {
        ProductModel productModel = JSONCopy.copy(productVo, ProductModel.class);
        /*int count = productDAO.update(productModel);
        ResultDTO resultDTO = ResultDTO.builder().count(count).build();
        return resultDTO;*/
        return ResultDTO.getDto(productMapper.update(productModel), null);
    }

    @Override
    public ResultDTO findAll(ProductVO productVo) {
        ProductModel productModel = JSONCopy.copy(productVo, ProductModel.class);
        productModel.setPage((productModel.getPage()-1)*productModel.getLimit());
        List<ProductModel> productModelList = productMapper.findAll(productModel);
        return ResultDTO.getDto(productMapper.count(productModel), productModelList);
    }

    @Override
    public ResultDTO findById(ProductVO productVo) {
        ProductModel productModel = JSONCopy.copy(productVo, ProductModel.class);
        return ResultDTO.getDto(0, productMapper.findById(productModel));
    }

    @Override
    public ResultDTO count(ProductVO productVo) {
        ProductModel productModel = JSONCopy.copy(productVo, ProductModel.class);
        return ResultDTO.getDto(productMapper.count(productModel), null);
    }

}

